package com.LeeCode;

import java.util.HashMap;
import java.util.HashSet;

/**
 * 相交链表
 **/

public class Code160 {
    public static void main(String[] args) {

    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> set = new HashSet<>();
        ListNode curA = headA;
        ListNode curB = headB;

        while (curA != null) {
            set.add(curA);
            curA = curA.next;
        }

        while (curB != null) {
            if (set.contains(curB))
                break;
            curB = curB.next;
        }
        return curB;
    }

    public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {

        if (headA == null || headB == null)
            return null;

        ListNode curA = headA;
        ListNode curB = headB;

        while (curB != curA) {
            curB = curB != null ? curB.next : headA;
            curA = curA != null ? curA.next : headB;
        }
        return curB;
    }
}


